
var func = require('../../utils/func.js');
var user = require('../../utils/user.js');

let uid;
let type;

Page({
    data:{
        id:"",
        time:"",
        userInfo:"",
        title:"",
        content:""
    },
    onLoad(e){
        let draft;
        let address;

        if(e.type == "display"){
            type = "display";
        }else{
            this.setData({
                id:e.id
            });
        }

        uid = user.userInfo.uid;

        if(type == "display"){
            address = "article_"+uid.toString();
        }else if(!!e.id){
            address = "draft_"+e.id.toString();
        }else {
            address = "draft_temp_"+uid.toString();
        }

        func.getStorge(address).then((res) => {
            draft = res.data;

            this.setData({
                content:draft.content,
                title:draft.title,
                userInfo:draft.userInfo,
                time:draft.time
            });
        }).catch((error) => {
            console.log(error);
        })
    },
    imageload(e){
        let id = e.currentTarget.dataset.id;
        let content = this.data.content;
        let height = e.detail.height;
        let width = e.detail.width;

        let cssHeight = (730 * (height/width)).toString()+"rpx";

        for(let i=0;i<content.length;i++){
            if(content[i].id == id){
                content[i].cssHeight = cssHeight;
                this.setData({
                    content:content
                })
                break;
            }
        }
    },
    previewImage(e){
        let src = e.target.dataset.src;

        let urls = [];
        let content = this.data.content;
        for(let i = 0;i<content.length;i++){
            if(content[i].type == "image"){
                urls.push(content[i].removeSrc);
            }
        }
        func.previewImage(src,urls);
    }
})